package com.dure.hotmemory.controllers;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.dure.commonlib.util.DbUtils;
import com.dure.commonlib.util.DeviceUtil;
import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.ToastUtils;
import com.dure.hotmemory.MainActivity;
import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.bean.FindPwd;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.bean.UserInfo;
import com.dure.hotmemory.config.Config;
import com.dure.hotmemory.drawview.BaseSurfaceView;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.request.RequestModel;
import com.dure.hotmemory.scenes.BaseScene;
import com.dure.hotmemory.utils.ShareUtil;
import com.socks.library.KLog;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.List;

/**
 * Created by dure on 2017/4/17.
 */

public class Controller {

    public BaseScene baseScene;
    public MainActivity activity;
    public BaseSurfaceView baseSurfaceView;
    public RequestModel requestModel;

    public Controller(BaseScene baseScene) {
        activity = baseScene.activity;
        baseSurfaceView = baseScene.baseSurfaceView;
        requestModel = new RequestModel(this);
    }

    public void handleRequestSuccess(Bean bean) {

    }

    public void checkPermission(String checkPermission, int requestCode, ConfirmListener confirmListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0版本以后的要进行动态授权
            if (activity.checkSelfPermission(checkPermission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (TextUtils.equals(checkPermission, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    activity.requestPermissions(new String[]{
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                            requestCode);
                } else if (TextUtils.equals(checkPermission, Manifest.permission.CAMERA)) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    activity.requestPermissions(new String[]{checkPermission,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                            requestCode);
                } else if (TextUtils.equals(checkPermission, Manifest.permission.READ_PHONE_STATE)) {
                    activity.requestPermissions(new String[]{checkPermission,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
                }

            } else {
                if (confirmListener != null)
                    confirmListener.confirmed(null);
            }
        } else {
            if (confirmListener != null)
                confirmListener.confirmed(null);
        }
    }

    public void checkPermission(int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0版本以后的要进行动态授权
            if (activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                //申请WRITE_EXTERNAL_STORAGE权限
                activity.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            } else {
                goToFileSelector(requestCode);
            }
        } else {
            goToFileSelector(requestCode);
        }
    }

    public void permissionGranted(){

    }

    private void goToFileSelector(int requestCode) {
//        Intent intent = new Intent(activity, FileSelectorActivity.class);
//        intent.putExtra(FileSelectorActivity.keyClassName, UniverseActivity.class.getName());
//        intent.putExtra(FileSelectorActivity.keyIsSelectFile, true);
//        intent.putExtra(FileSelectorActivity.keyIsSingleSelector, true);
//        activity.startActivityForResult(intent, requestCode);
    }

    public void doThirdLogin(String loginType) {

        SHARE_MEDIA share_media = SHARE_MEDIA.QQ;
        if (TextUtils.equals(loginType, "QQ")) {
            share_media = SHARE_MEDIA.QQ;
        } else if (TextUtils.equals(loginType, "WEIXIN")) {
            share_media = SHARE_MEDIA.WEIXIN;
        } else if (TextUtils.equals(loginType, "SINA")) {
            share_media = SHARE_MEDIA.SINA;
        }
        final SHARE_MEDIA share_media_tem = share_media;
        ShareUtil.doOauthVerify(activity, share_media, new ConfirmListener() {
            @Override
            public void confirmed(Bundle bundle) {

                String uid = bundle.getString("uid");
                String name = bundle.getString("name");
                String gender = bundle.getString("gender");
                String iconurl = bundle.getString("iconurl");

                PreferencesUtils.putBoolean(activity, "login_success", true);
                PreferencesUtils.putString(activity, "uid", uid);
                PreferencesUtils.putString(activity, "name", name);
                PreferencesUtils.putString(activity, "sex", gender);
                PreferencesUtils.putString(activity, "head_img", iconurl);
                // 三方登录成功，查看服务器是否有用户数据
                getUserInfoByUid(uid);
            }
        });
    }

    public void getUserInfoByUid(String uid) {
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", uid);
        paramMap.put("device_id", DeviceUtil.getDeviceId(activity));

        requestModel.doPost(Config.HOST + Config.USER_INFO, paramMap, UserInfo.class);
    }

    /**
     * 插入三方登录用户信息
     */
    public void insertThirdLoginUserInfo(String uid, String name, String gender, String iconUrl) {

        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", uid);
        paramMap.put("name", name);
        int sex = TextUtils.equals("男", gender) ? 1 : 0;
        paramMap.put("sex", String.valueOf(sex));
        paramMap.put("image", iconUrl);
        paramMap.put("device_id", DeviceUtil.getDeviceId(activity));
        paramMap.put("register_time", String.valueOf(System.currentTimeMillis() / 1000));

        requestModel.doPost(Config.HOST + Config.ADD_USER_INFO, paramMap);

    }


    public void doRegister(String passwordStr, String phoneStr, String codeStr) {// 注册
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phoneStr);
        paramMap.put("password", passwordStr);
        paramMap.put("code", codeStr);
        paramMap.put("device_id", DeviceUtil.getDeviceId(activity));
        paramMap.put("register_time", String.valueOf(System.currentTimeMillis() / 1000));

        requestModel.doPost(Config.HOST + Config.REGIST, paramMap, UserInfo.class);
    }

    public void doLogin(String phoneStr, String passwordStr) {
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phoneStr);
        paramMap.put("password", passwordStr);
        paramMap.put("device_id", DeviceUtil.getDeviceId(activity));

        requestModel.doPost(Config.HOST + Config.LOGIN, paramMap, UserInfo.class);
    }

    public void doFindPwd(String passwordStr, String phoneStr, String codeStr) {
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phoneStr);
        paramMap.put("password", passwordStr);
        paramMap.put("code", codeStr);

        requestModel.doPost(Config.HOST + Config.FIND_PWD, paramMap, FindPwd.class);
    }

    public void getPhoneCode(String phoneStr) {// 验证码
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phoneStr);
        requestModel.doPost(Config.HOST + Config.GET_CODE, paramMap);
    }

    public void uploadTrainingData(String level,String trainingName) {
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        else if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));
        paramMap.put("level", level);
        paramMap.put("user_name", PreferencesUtils.getString(activity, "name"));
        paramMap.put("training_name", trainingName);
        requestModel.doPost(Config.HOST + Config.UPDATE_LEVEL, paramMap);
    }

}
